<template>
    <div class="optionInput">
        <el-select v-model="optionValue" style="margin-bottom:10px" @change='getValue'>
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
        </el-select>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                optionValue: 1,
                obj: {},
                options: [{
                    value: 1,
                    label: '今天'
                }, {
                    value: 2,
                    label: '昨天'
                }, {
                    value: 3,
                    label: '本周'
                }, {
                    value: 4,
                    label: '上周'
                }, {
                    value: 5,
                    label: '本月'
                }, {
                    value: 6,
                    label: '上月'
                }, {
                    value: 7,
                    label: '近三月'
                }],
                start:'yyyy-MM-dd 00:00:00',
                end:'yyyy-MM-dd 23:59:59',
            }
        },
        props: {
            label:[Array, Number],
            comData: {
                type: Object,
                default: () => {}
            },
        },
        mounted(){
            this.enumComData(1)
            this.obj.compType = 1
            this.$emit('setFormData', this.obj)
        },
        methods: {
            // type "s"代表开始,"e"代表结束，dates不传或0代表本周，-1代表上周，1代表下周
            getMonday(type, dates) {
                let now = new Date();
                let nowTime = now.getTime();
                let day = now.getDay();
                let longTime = 24 * 60 * 60 * 1000;
                let n = longTime * 7 * (dates || 0);
                let dd
                if (type == "s") {
                    dd = nowTime - (day - 1) * longTime + n;
                };
                if (type == "e") {
                    dd = nowTime + (7 - day) * longTime + n;
                };
                dd = new Date(dd);
                let y = dd.getFullYear();
                let m = dd.getMonth() + 1;
                let d = dd.getDate();
                m = m < 10 ? "0" + m : m;
                d = d < 10 ? "0" + d : d;
                day = y + "-" + m + "-" + d;
                return day;
            },
            // type "s"代表开始,"e"代表结束，dates不传或0代表本月，-1代表上月，1代表下月
            getMonth(type, months) {
                let d = new Date();
                let year = d.getFullYear();
                let month = d.getMonth() + 1;
                if (Math.abs(months) > 12) {
                    months = months % 12;
                };
                if (months != 0) {
                    if (month + months > 12) {
                        year++;
                        month = (month + months) % 12;
                    } else if (month + months < 1) {
                        year--;
                        month = 12 + month + months;
                    } else {
                        month = month + months;
                    };
                };
                month = month < 10 ? "0" + month : month;
                let firstday = year + "-" + month + "-" + "01";
                let lastday = "";
                if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month ==
                    "10" || month == "12") {
                    lastday = year + "-" + month + "-" + 31;
                } else if (month == "02") {
                    if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
                        lastday = year + "-" + month + "-" + 29;
                    } else {
                        lastday = year + "-" + month + "-" + 28;
                    };
                } else {
                    lastday = year + "-" + month + "-" + 30;
                };
                let day = "";
                if (type == "s") {
                    day = firstday;
                } else {
                    day = lastday;
                };
                return day;
            },
            //近三月起始时间
            beforeThree() {
                let dates = new Date()
                dates.setMonth(dates.getMonth() - 3)
                    var pastMonth = dates.getMonth() + 1
                    var pastDay = dates.getDate()
                    if (pastMonth >= 1 && pastMonth <= 9) {
                    pastMonth = '0' + pastMonth
                    }
                    if (pastDay >= 0 && pastDay <= 9) {
                    pastDay = '0' + pastDay
                    }
                let firstDay = dates.getFullYear() + '-' + pastMonth + '-' + pastDay
                return firstDay
            },
            enumComData(val){
                const {i_key,name,type} = this.comData
                this.obj.key = i_key
                this.obj.chineseName = name
                this.obj.type = type
                this.obj.dynamicType = val
                this.optionValue = val
                this.getValue(val)
            },
            getDynamicType(val){
                this.enumComData(val)
                this.$emit('setFormData', this.obj)
            },
            getValue(val) {
                const {i_key} = this.comData
                const {formatTime} = this.utils
                if(val == 2){
                    this.obj[i_key] = [formatTime(new Date().getTime() - 3600 * 1000 * 24,this.start),
                        formatTime(new Date().getTime() - 3600 * 1000 * 24,this.end)]
                }else if(val == 3){
                    this.obj[i_key] = [formatTime(this.getMonday("s", -0), this.start),formatTime(this.getMonday("e", -0), this.end)]
                }else if(val == 4){
                    this.obj[i_key] = [formatTime(this.getMonday("s", -1), this.start),formatTime(this.getMonday("e", -1), this.end)]
                }else if(val == 5){
                    this.obj[i_key] = [formatTime(this.getMonth("s", 0), this.start),formatTime(this.getMonth("e", 0), this.end)]
                }else if(val == 6){
                    this.obj[i_key] = [formatTime(this.getMonth("s", -1), this.start),formatTime(this.getMonth("e", -1), this.end)]
                }else if(val == 7){
                    this.obj[i_key] = [formatTime(this.beforeThree(), this.start),new Date().dateFormat(this.end)]
                }else{
                    this.obj[i_key] = [new Date().dateFormat(this.start),new Date().dateFormat(this.end)]
                }
                this.obj.dynamicType = val
            }
        }
    }
</script>